// 登录
homeApp.controller("loginCtr",function($scope,$location,commonService,userService){
	$(".start_loading").addClass("loading");

	if($(window).width() >= 1000){
		$scope.showNav = true;
	}else{
		$scope.showNav = false;
	}

	$scope.input = function(){

		if(userService.isPc()){
			if($scope.mobile.length <= 4){
				$scope.mobile = $scope.mobile.replace(/(\d{3})(?=\d+)/,"$1 ");
			}else if( $scope.mobile.length > 5 && $scope.mobile.length <= 9 ){
				$scope.mobile = $scope.mobile.replace(/([0-9 ]{8})(?=\d+)/,"$1 ");
			}else if($scope.mobile.length == 11){
				$scope.mobile = $scope.mobile.replace(/(\d{3})(\d{4})(\d{4})/,"$1 $2 $3");
			}
		}

		if( $scope.mobile.length > 13 ){
			$scope.mobile = $scope.mobile.substr(0,13);
		}
	}

	$scope.loginClick = function(){
		login($scope.mobile.replace(/\s/g,''),$scope.password);
	}

	$scope.loginKeyUp = function($event){
		var keycode = window.event?$event.keyCode:$event.which;
		if(keycode == 13){
			login($scope.mobile.replace(/\s/g,''),$scope.password);
		}
	}

	var login = function(mobile,password){
		if("undefined" == typeof $scope.mobile){
				alert("手机号不能为空！");
				return false;
			}

			if("undefined" == typeof $scope.password || !userService.checkPassword($scope.password)){
				alert("密码输入错误！");
				return false;
			}

			var formDataStr = $.param({
				phone: mobile,
	     		password: password,
	    	})

			$(".start_loading").addClass("loading");
			commonService.httpReq(loginUrl,formDataStr,function(response){
				if(response.STATUS == SUCCESS){
					userService.setLocalStorage("start-uuid",response.RESULT.uuid);
					userService.setLocalStorage("start-name",response.RESULT.name);
					userService.setLocalStorage("start-pic",response.RESULT.pic);
					$location.path("/home/index");
				}else{
					alert(response.ERRMSG);
					$(".start_loading").removeClass("loading");
				}
			})
	}

	$(".start_loading").removeClass("loading");
})

// 注册
homeApp.controller("registerCtr",function($scope,$location,commonService,userService){
	$(".start_loading").addClass("loading");
	$scope.disable = false;
	$scope.registerAble = false;
	$scope.readonly = false;

	if($(window).width() >= 1000){
		$scope.showNav = true;
	}else{
		$scope.showNav = false;
	}

	$scope.input = function(){
		if(userService.isPc()){
			if($scope.mobile.length <= 4){
				$scope.mobile = $scope.mobile.replace(/(\d{3})(?=\d+)/,"$1 ");
			}else if( $scope.mobile.length > 5 && $scope.mobile.length <= 9 ){
				$scope.mobile = $scope.mobile.replace(/([0-9 ]{8})(?=\d+)/,"$1 ");
			}else if($scope.mobile.length == 11){
				$scope.mobile = $scope.mobile.replace(/(\d{3})(\d{4})(\d{4})/,"$1 $2 $3");
			}

			if( $scope.mobile.length > 13 ){
				$scope.mobile = $scope.mobile.substr(0,13);
			}
		}else {
			if( $scope.mobile.length > 11 ){
				$scope.mobile = $scope.mobile.substr(0,11);
			}
		}

		
	}

	$scope.sendVertify = function(){

		if(!userService.checkPhoneNum($scope.mobile.replace(/ /g,'')) || "undefined" == typeof $scope.mobile){
			alert("笨蛋，手机号输入错啦~");
			return false;
		}

		$scope.disable = true;
		$scope.readonly = true;
		var time = 120;
		var formDataStr = $.param({
			phone: $scope.mobile.replace(/\s/g,''),
		})

		commonService.httpReq(sendVertifyUrl,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				alert("验证码已经发送到您的手机啦！");
				$scope.registerAble = true;
				var clock = setInterval(function(){
					$(".start_vcodebtn").text("("+time+")s后再次获取");
					time--;
					if(time == -1){
						$scope.disable = false;
						$scope.readonly = false;
						$(".start_vcodebtn").text("获取验证码")
					}
				},1000)
			}else{
				alert(response.ERRMSG);
				$scope.disable = false;
				$scope.readonly = false;
				//$scope.registerAble = true;
			}
		})
	}

	$scope.register = function(){

		if("undefined" == typeof $scope.password || !userService.checkPassword($scope.password)){
			alert("密码不能为空并且要以字母、数字或下划线组合，且至少6位！");
			return false;
		}

		if("undefined" == typeof $scope.confirmPassword || !userService.checkPassword($scope.confirmPassword) || $scope.confirmPassword != $scope.password){
			alert("确认密码与您输入的密码不相符！");
			return false;
		}

		if( "undefined" == typeof $scope.vcode ){
			alert("验证码不能为空！");
			return false;
		}

		var formDataStr = $.param({
			phone: $scope.mobile.replace(/\s/g,''),
     		password: $scope.password,
     		comfirm: $scope.confirmPassword,
    		vertify: $scope.vcode
    	})

		$(".start_loading").addClass("loading");
		commonService.httpReq(registerUrl,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				userService.setLocalStorage("start-uuid",response.RESULT.uuid);
				userService.setLocalStorage("start-name",response.RESULT.name);
				userService.setLocalStorage("start-pic",response.RESULT.pic);
				$location.path("/home/index");
			}else{
				alert(response.ERRMSG);
				$(".start_loading").removeClass("loading");
			}
		})
	}

	$(".start_loading").removeClass("loading");
})

// 服务条款
homeApp.controller("serviceListCtr",function($scope,$location,commonService,userService){
	$(".start_loading").addClass("loading");
	$(".start_loading").removeClass("loading");
})

// 个人中心 - mobile
homeApp.controller("mindexCtr",function($scope,$location,commonService,userService){
	$(".start_loading").addClass("loading");
	if(userService.userIsLogin()){
		$scope.userIsLogin = true;
		var formDataStr = $.param({
			uuid:userService.getLocalStorage("start-uuid"),
		})

		commonService.httpReq(getUserInfoUrl,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				var res = response.RESULT;
				res.pic = urlPic + res.pic;
				$scope.info = res;
				$(".start_loading").removeClass("loading");
			}
		})

		$scope.logout = function(){
			var logoutConfirm = confirm("您确定要退出吗？");
			if(logoutConfirm){
				localStorage.removeItem("start-uuid");
				localStorage.removeItem("start-name");
				localStorage.removeItem("start-pic");
				alert("退出成功！");
				$location.path("/user/login");
				$scope.isLogin = false;
			}
		}

	}else{
		$scope.userIsLogin = false;
		alert("您还未登录！");
		$location.path("/user/login")
	}
})

// 修改用户名
homeApp.controller("modifyNameCtr",function($scope,$location,commonService,userService){
	$(".start_loading").addClass("loading");

	if(userService.userIsLogin()){
		$scope.userIsLogin = true;
	}else{
		$scope.userIsLogin = false;
		alert("您还未登录！");
		$location.path("#/user/login");
	}

	$scope.submit = function(){

		var reg = /^[\u4E00-\u9FA5\uf900-\ufa2d\w]{1,5}$/;
		if(!reg.test($scope.name)){
			alert("请输入包含汉字、英文的昵称，并不多于5个字！");
			return false;
		}

		var formDataStr = $.param({
			name: $scope.name,
			uuid: userService.getLocalStorage("start-uuid")
		})

		commonService.httpReq(modifyNameUrl,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				alert("修改成功！");
				$location.path("/user/mindex");
				userService.setLocalStorage("start-name",$scope.name);
			}else{
				alert(response.ERRMSG);
				return false;
			}
		})
	}
	$(".start_loading").removeClass("loading");
})


// 忘记密码
homeApp.controller("modifyPasswordCtr",function($scope,$location,commonService,userService){
	$(".start_loading").addClass("loading");
	$scope.disable = false;
	$scope.registerAble = false;
	$scope.readonly = false;
	var clock="";
	if("undefined" != typeof clock){
		clearInterval(clock);
	}

	$scope.input = function(){
		if(userService.isPc()){
			if($scope.mobile.length <= 4){
				$scope.mobile = $scope.mobile.replace(/(\d{3})(?=\d+)/,"$1 ");
			}else if( $scope.mobile.length > 5 && $scope.mobile.length <= 9 ){
				$scope.mobile = $scope.mobile.replace(/([0-9 ]{8})(?=\d+)/,"$1 ");
			}else if($scope.mobile.length == 11){
				$scope.mobile = $scope.mobile.replace(/(\d{3})(\d{4})(\d{4})/,"$1 $2 $3");
			}

			if( $scope.mobile.length > 13 ){
				$scope.mobile = $scope.mobile.substr(0,13);
			}
		}else {
			if( $scope.mobile.length > 11 ){
				$scope.mobile = $scope.mobile.substr(0,11);
			}
		}

		
	}

	$scope.sendVertify = function(){

		if(!userService.checkPhoneNum($scope.mobile.replace(/ /g,'')) || "undefined" == typeof $scope.mobile){
			alert("笨蛋，手机号输入错啦~");
			return false;
		}

		$scope.disable = true;
		$scope.readonly = true;
		var time = 120;
		var formDataStr = $.param({
			phone: $scope.mobile.replace(/\s/g,''),
		})

		commonService.httpReq(findPsSendCode,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				alert("验证码已经发送到您的手机啦！");
				$scope.registerAble = true;
				clock = setInterval(function(){
					$(".start_vcodebtn").text("("+time+")s后再次获取");
					time--;
					if(time == -1){
						$scope.disable = false;
						$scope.readonly = false;
						$(".start_vcodebtn").text("获取验证码")
					}
				},1000)
			}else{
				alert(response.ERRMSG);
				$scope.disable = false;
				$scope.readonly = false;
				//$scope.registerAble = true;
			}
		})
	}

	$scope.register = function(){

		if("undefined" == typeof $scope.password || !userService.checkPassword($scope.password)){
			alert("密码不能为空并且要以字母、数字或下划线组合，且至少6位！");
			return false;
		}

		if("undefined" == typeof $scope.confirmPassword || !userService.checkPassword($scope.confirmPassword) || $scope.confirmPassword != $scope.password){
			alert("确认密码与您输入的密码不相符！");
			return false;
		}

		if( "undefined" == typeof $scope.vcode ){
			alert("验证码不能为空！");
			return false;
		}

		var formDataStr = $.param({
			phone: $scope.mobile.replace(/\s/g,''),
     		password: $scope.password,
     		comfirm: $scope.confirmPassword,
    		vertify: $scope.vcode
    	})

		$(".start_loading").addClass("loading");
		commonService.httpReq(changePs,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				clearInterval(clock);
				alert("修改成功！");
				$location.path("/user/login");
			}else{
				alert(response.ERRMSG);
				$(".start_loading").removeClass("loading");
			}
		})
	}

	$(".start_loading").removeClass("loading");
})


